<template>
  <div>
    <page-list :x-uri="uri" :x-params="{ is_recommend: 1 }" :custom-components="custom" :doactions="doactions">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <tabs />
      </template>
      <!-- 多选操作 -->
      <template v-slot:table-button="scope">
        <el-button v-auth="'course.live.rec_small.edit'" size="mini" @click="cancelRecommend(scope)">取消推荐</el-button>
      </template>
    </page-list>
    <sort-dialog :value.sync="sortValue" :visible.sync="visible" :disable-submit="disableSubmit" @submit="onSubmit" />
  </div>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import Status from './components/status';
import UpdateStatus from './components/update_status';
import Titile from '../video/components/title.vue';
import IsRecommend from './components/is_recommend.vue';
import CreditRedeem from './components/credit_redeem.vue';
import Tabs from '../live/components/tabs.vue';
import SortDialog from '@/components/update-sort-dialog';
import CategoryTitle from '@/components/category-title'

export default {
  components: {
    PageList,
    Tabs,
    SortDialog
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'course/admin/live/small/list',
      custom: {
        status: Status,
        update_status: UpdateStatus,
        title: Titile,
        is_recommend: IsRecommend,
        credit_redeem: CreditRedeem,
        cates: CategoryTitle
      },
      doactions: [
        {
          title: '课时管理',
          meta: { permission: 'course.live.rec_small.section' },
          display: ({ row }) => {
            return row.update_status == 1;
          },
          callback: this.section
        },
        { title: '学员', meta: { permission: 'course.live.rec_small.student' }, callback: this.student },
        {
          title: '课件',
          meta: { permission: 'course.live.rec_small.courseware' },
          callback: this.courseware
        },
        { title: '排序', meta: { permission: 'course.live.rec_small.order' }, callback: this.sortPopover },
        {
          title: '编辑',
          meta: { permission: 'course.live.rec_small.edit' },
          // display: ({ row }) => {
          //   return row.publish_status == 0;
          // },
          callback: this.edit
        },
        { title: '考试', meta: { permission: 'course.live.rec_small.exam' }, callback: this.exam },
        { title: '评论', meta: { permission: 'course.live.rec_small.comment' }, callback: this.comment },
        { title: '笔记', meta: { permission: 'course.live.rec_small.note' }, callback: this.note },
        { title: '收藏', meta: { permission: 'course.live.rec_small.collect' }, callback: this.collection },
        { title: '加购', meta: { permission: 'course.live.rec_small.present' }, callback: this.cart },
        { title: '复制', meta: { permission: 'course.live.rec_small.copy' }, callback: this.copyCourse },
        { title: '删除', meta: { permission: 'course.live.rec_small.delete' }, callback: this.delete }
      ],
      itemId: 0,
      visible: false,
      disableSubmit: false,
      sortValue: 1 // 排序值
    };
  },
  methods: {
    section: function({ row }) {
      this.$router.push({ path: '/course/live/small/' + row.id + '/section' });
    },
    edit: function({ row }) {
      this.$router.push({ path: '/course/live/small/' + row.id + '/edit' });
    },
    // 考试
    exam: function({ row }) {
      this.$router.push({ path: '/course/exam/' + row.id + '/list' });
    },
    // 评论
    comment: function({ row }) {
      this.$router.push({ path: '/course/comment/' + row.id + '/list' });
    },
    // 笔记
    note: function({ row }) {
      this.$router.push({ path: '/course/note/' + row.id + '/list' });
    },
    // 收藏
    collection: function({ row }) {
      this.$router.push({ path: '/course/collection/' + row.id + '/list' });
    },
    // 加购
    cart: function({ row }) {
      this.$router.push({ path: '/course/cart/' + row.id + '/list' });
    },
    // 复制课程
    copyCourse: function ({ row }) {
      this.$box.confirm('确认复制课程信息吗').then(() => {
        this.$http.post(
            '/course/admin/' + row.id + '/copy',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
        );
      }).catch(() => {})
    },
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/live/small/' + row.id,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    student: function({ row }) {
      this.$router.push({ path: '/course/live/small/' + row.id + '/student' });
    },
    // 课件
    courseware: function({ row }) {
      this.$router.push({ path: '/course/courseware/' + row.id + '/list' });
    },
    // 排序弹出层
    sortPopover({ row }) {
      this.visible = true;
      this.itemId = row.id
      this.sortValue = row.best_sort
    },
    // 保存
    onSubmit() {
      this.disableSubmit = true;
      this.$http.put('/course/admin/live/small/' + this.itemId + '/edit/best_sort', {
        best_sort: this.sortValue
      }, ({ msg }) => {
        this.$message.success(msg);
        this.disableSubmit = false;
        this.visible = false;
        this.reload();
      }, ({ msg }) => {
        this.$message.error(msg)
        this.disableSubmit = false;
      }, false)
    },
    // 批量下架
    cancelRecommend({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要取消推荐的课程');
        return false;
      }
      this.$box
          .confirm('确认取消推荐吗')
          .then(() => {
            this.$http.put(
                '/course/admin/batch/cancel/recommend',
                {
                  ids: ids
                },
                response => {
                  this.$message.success(response.msg, _ => {
                    this.reload();
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {});
    }
  }
};
</script>
